package com.coder.algorithm.leetcode

import com.coder.algorithm.struct.ListNode

/**
 * [203. 移除链表元素](https://leetcode-cn.com/problems/remove-linked-list-elements/)
 * 列表中的节点数目在范围 [0, 104] 内
 * 1 <= Node.val <= 50
 * 0 <= val <= 50
 *
 * @author yuhus
 * @date: 2022/4/2
 */
object l_0203 {
    /**
     * dummyHead: 假的，傀儡
     */
    fun removeElements(head: ListNode?, `val`: Int): ListNode? {
        var preHead = ListNode(0)
        preHead.next = head
        var prev: ListNode? = preHead
        while (prev?.next != null) {
            if (prev.next?.`val` == `val`) {
                prev.next = prev.next?.next
            } else {
                prev = prev.next
            }
        }
        return preHead.next
    }
}